<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */

defined('IN_ADMIN_COMPONENT_CONTROLLER') or die();

require_once('includes/classes/Upload.php');

class CategoriesProductsComponent
{
	function CategoriesProductsComponent()
	{
	}

	function action(&$params, $action)
	{
		$params['newLink'] = false;
		$this->message = array();
		$this->errorCode = 0;
		if ($action == 'SaveCategory') 
			return $this->editCategory($params);
		else if ($action == 'DeleteCategory') 
			return $this->deleteCategory($params);
		else if ($action == 'MoveCategory') 
			return $this->moveCategory($params);
		else if ($action == 'DeleteProduct') 
			return $this->deleteProduct($params);
		else if ($action == 'CopyProduct') 
			return $this->CopyProduct($params);
		else if ($action == 'MoveProduct') 
			return $this->moveProduct($params);
		else if ($action == 'NewProductLink') 
			return $this->newProductLink($params);
		else if ($action == 'ToggleProductStatus')
			return $this->toggleProductStatus($params);
		else
			return $this->query($params);
	}

	function query(&$params) 
	{
		$handle = get_class($this);
		$category_id = (int)$params['categoryId'];
		$language_id = (int)$params['languageId'];
		$manufacturer_id = (int)$params['manufacturerId'];
		$selected_category_id = (int)$params['selectedCategoryId'];

		$sqlQuery = 'select c.categories_id, cd.categories_name, c.categories_image, c.parent_id, count(p.products_id) as products_count, sort_order,' .
		"DATE_FORMAT(date_added, '%Y-%m-%d %H:%i:%s') as date_added, " .
		"DATE_FORMAT(last_modified, '%Y-%m-%d %H:%i:%s') as last_modified " .
		'from ' . TABLE_CATEGORIES_DESCRIPTION . ' cd, ' . TABLE_CATEGORIES . ' c ' .
		'left join (' . TABLE_PRODUCTS_TO_CATEGORIES . ' pc, ' . TABLE_PRODUCTS . ' p) ' .
		'on (c.categories_id = pc.categories_id and pc.products_id = p.products_id and p.products_status=\'1\'';
				
	 	$hideEmpty = false;
		if ($manufacturer_id > 0) {
			$hideEmpty = true;
			$sqlQuery .= ' and p.manufacturers_id = \'' . $manufacturer_id . '\'';
		}

		$sqlQuery .= ') ';

		$sqlQuery .= 'where c.categories_id = cd.categories_id and cd.language_id=\'' . $language_id . '\' ';

		if($category_id > 0) $sqlQuery .= ' and c.categories_id = ' . $category_id . ' ';

		$sqlQuery .= 'group by c.categories_id ' .
		'order by c.parent_id, c.sort_order, cd.categories_name';

		$dataReturned = mysql_query($sqlQuery) or die(mysql_error());

		$value{$handle . 'MetaData'}{'store_name'}= STORE_NAME;
		$value{$handle . 'MetaData'}{'image_width_max'}= (int)SUBCATEGORY_IMAGE_WIDTH;
		$value{$handle . 'MetaData'}{'image_height_max'}= (int)SUBCATEGORY_IMAGE_HEIGHT;

		$i = 0;
		while($row = mysql_fetch_array($dataReturned))
		{
			$current_category_id = (int)$row['categories_id'];
			$products_count = (int)$row['products_count'];
			$value{$handle}{$i}{'id'}= $current_category_id;
			$value{$handle}{$i}{'name'}= $row['categories_name'];
			$value{$handle}{$i}{'image'}= $row['categories_image'];
			$value{$handle}{$i}{'parent_id'}= (int)$row['parent_id'];
			$value{$handle}{$i}{'count'}= $products_count;
			$value{$handle}{$i}{'sort_order'}= (int)$row['sort_order'];
			$value{$handle}{$i}{'date_added'}= $row['date_added'];
			$value{$handle}{$i}{'last_modified'}= $row['last_modified'];

			$imagefile = DIR_FS_CATALOG_IMAGES . 'products/' . $row['categories_image'];
			$image_size = @getimagesize($imagefile);
			$value{$handle}{$i}{'image_width'}= $image_size[0];
			$value{$handle}{$i}{'image_height'}= $image_size[1];
		
			if ($category_id > 0)
			{
				$value{$handle}{$i}{'error_code'}= $this->errorCode;
				$value{$handle}{$i}{'message'}= $this->message;
			}
			
			if ($selected_category_id > 0 && $selected_category_id == $current_category_id)
			{
				$subParams['handle'] = 'Products';
				$subParams['languageId'] = (int)$params['languageId'];
				$subParams['manufacturerId'] = (int)$params['manufacturerId'];
				$subParams['categoryId'] = $current_category_id;
				$subParams['max_rows'] = 10000;
				$subParams['orderby'] = 'pd.products_name';
				$subParams['include_all_status'] = true;
			
				require_once('includes/components/ProductsSearchSubComponent.php');
				$subComponent = new ProductsSearchSubComponent();
				$return = $subComponent->query($subParams);
				$products = $return['Products'];
				if ($products != null) $value{$handle}{$i}{'products'}= $products;
			}
			$i++;
		}
		return $value;
	}

	function editCategory(&$params) 
	{
		$language_id = (int)$params['languageId'];
		$category_id = (int)$params['categoryId'];
		$category_parent_id = (int)$params['parentCategoryId'];
		$category_name = trusted_prepare_input($params['categoryName']);
		$sort_order = (int)$params['sortOrder'];

		$sqlAction = 'insert';
		$where = '';
		if ($category_id > 0)
		{
			$sqlAction = 'update';
			$where = 'categories_id=' . $category_id;
		}

		$sql_data_array = array();
		if ($sqlAction == 'insert')
		{

			//New Parent has no Products
			$sqlQuery = 'select categories_id from ' . TABLE_PRODUCTS_TO_CATEGORIES . ' where categories_id=' . $category_parent_id;
			$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
			$i = 0;
			while($row = mysql_fetch_array($dataReturned))
			{
				$i++;
				break;
			}
			
			if ($i > 0)
			{
				$this->errorCode = -100;
				$this->message = 'PARENT_CONTAINS_PRODUCTS';
				return $this->query($params);
			}
	
			//Parent Exists
			if ($category_parent_id == 0)  {
				$j=1;
			}
			else {
				$sqlQuery = 'select categories_id from ' . TABLE_CATEGORIES . ' where categories_id=' . $category_parent_id;
				$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
				$j = 0;
				while($row = mysql_fetch_array($dataReturned))
				{
					$j++;
				}
			}

			if ($j == 0)
			{
				$this->errorCode = -110;
				$this->message = 'PARENT_DOES_NOT_EXIST';
				return $this->query($params);
			}

			$sql_data_array['categories_id'] = $category_id;
			$sql_data_array['parent_id'] = $category_parent_id;
			$sql_data_array['date_added'] = 'now()';
		}
		$sql_data_array['sort_order'] = $sort_order;
		$sql_data_array['last_modified'] = 'now()';

		db_perform(TABLE_CATEGORIES, $sql_data_array, $sqlAction, $where);

		if ($sqlAction == 'insert')
		{
				$category_id = mysql_insert_id();
		}
		else
		{
			$where = 'categories_id=' . $category_id . ' and language_id=' . $language_id;
		}

		$sql_data_array = array();
		if ($sqlAction == 'insert')
		{
			$sql_data_array['categories_id'] = $category_id;
			$sql_data_array['language_id'] = $language_id;
		}
		$sql_data_array['categories_name'] = $category_name;
		db_perform(TABLE_CATEGORIES_DESCRIPTION, $sql_data_array, $sqlAction, $where);

		$params['categoryId'] = $category_id;
		$params['selectedCategoryId'] = $category_id;

        	$category_image = new Upload('categoryImage', DIR_FS_CATALOG_IMAGES . 'products/', false);

		if ($category_image->parse() && $category_image->save()) {
			$sql = 'update ' . TABLE_CATEGORIES . ' set categories_image = \'' . mysql_real_escape_string($category_image->filename) . '\' where categories_id = ' . $category_id;
			mysql_query($sql) or die(mysql_error());
		}
		$this->errorCode = $category_image->error_code;
		$this->message = $category_image->message;

		clean_cache('CategoriesComponent');

		return $this->query($params);
	}

	function deleteCategory(&$params) 
	{
		$category_id = (int)$params['categoryId'];

		$sqlQuery = 'select categories_id from ' . TABLE_PRODUCTS_TO_CATEGORIES . ' where categories_id=' . $category_id;
		$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
		$i = 0;
		while($row = mysql_fetch_array($dataReturned))
		{
			$i++;
		}
			
		if ($i > 0)
		{
			$this->errorCode = -120;
			$this->message = 'CATEGORY_CONTAINS_PRODUCTS';
			return $this->query($params);
		}

		$sqlQuery = 'select categories_id from ' . TABLE_CATEGORIES . ' where parent_id=' . $category_id;
		$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
		while($row = mysql_fetch_array($dataReturned))
		{
			$i++;
		}
			
		if ($i > 0)
		{
			$this->errorCode = -130;
			$this->message = 'CATEGORY_CONTAINS_CATEGORIES';
			return $this->query($params);
		}

		if ($i == 0)
		{
			$sql = 'delete from ' . TABLE_CATEGORIES . ' where categories_id=' . $category_id;
			mysql_query($sql) or die(mysql_error());
			$sql = 'delete from ' . TABLE_CATEGORIES_DESCRIPTION . ' where categories_id=' . $category_id;
			mysql_query($sql) or die(mysql_error());
			$params['categoryId'] = 0;
			clean_cache('CategoriesComponent');
		}

		return $this->query($params);
	}

	function moveCategory(&$params) 
	{
		$category_id = (int)$params['categoryId'];
		$category_parent_id = (int)$params['selectedCategoryId'];

		if ($category_id > 0)
		{
			$sqlQuery = 'select categories_id from ' . TABLE_PRODUCTS_TO_CATEGORIES . ' where categories_id=' . $category_parent_id;
			$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
			$i = 0;
			while($row = mysql_fetch_array($dataReturned))
			{
				$i++;
				break;
			}
			
			if ($i > 0)
			{
				$this->errorCode = -100;
				$this->message = 'PARENT_CONTAINS_PRODUCTS';
				return $this->query($params);
			}
	
			if ($category_parent_id == 0)  {
				$j=1;
			}
			else {
				$sqlQuery = 'select categories_id from ' . TABLE_CATEGORIES . ' where categories_id=' . $category_parent_id;
				$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
				$j = 0;
				while($row = mysql_fetch_array($dataReturned))
				{
					$j++;
				}
			}

			if ($j == 0)
			{
				$this->errorCode = -110;
				$this->message = 'PARENT_DOES_NOT_EXIST';
				return $this->query($params);
			}

			$sqlQuery = 'Update ' . TABLE_CATEGORIES . ' set parent_id=' . $category_parent_id . ' where categories_id =' . $category_id;
			mysql_query($sqlQuery) or die(mysql_error());
			clean_cache('BestSellersComponent');
			clean_cache('CategoriesComponent');
			clean_cache('NewProductsComponent');
			clean_cache('RandomReviewComponent');
			clean_cache('SpecialsComponent');
			clean_cache('UpcomingProductsComponent');
			clean_cache('WhatsNewComponent');
		}
		$params['categoryId'] = 0;
		$params['selectedCategoryId'] = $category_parent_id;


		return $this->query($params);
	}

	function deleteProduct(&$params) 
	{
		$product_id = (int)$params['productId'];
		$selected_category_id = (int)$params['selectedCategoryId'];


		$sqlQuery = 'select products_id from ' . TABLE_PRODUCTS_TO_CATEGORIES . ' where products_id =' . $product_id;
		$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
		$i = 0;
		while($row = mysql_fetch_array($dataReturned))
		{
			$i++;
		}
			
		if ($i > 1) {
			$sql = 'delete from ' . TABLE_PRODUCTS_TO_CATEGORIES . ' where categories_id=' . $selected_category_id . ' and products_id =' . $product_id;
			mysql_query($sql) or die(mysql_error());
		}
		else {
			$sql = 'delete from ' . TABLE_CUSTOMERS_BASKET . ' where products_id=' . $product_id;
			mysql_query($sql) or die(mysql_error());
			$sql = 'delete from ' . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . ' where products_id=' . $product_id;
			mysql_query($sql) or die(mysql_error());
			$sql = 'delete from ' . TABLE_PRODUCTS . ' where products_id=' . $product_id;
			mysql_query($sql) or die(mysql_error());
			$sql = 'delete from ' . TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD . ' where products_attributes_id in (select products_attributes_id from ' . TABLE_PRODUCTS_ATTRIBUTES . ' where products_id=' . $product_id . ')';
			mysql_query($sql) or die(mysql_error());
			$sql = 'delete from ' . TABLE_PRODUCTS_ATTRIBUTES . ' where products_id=' . $product_id;
			mysql_query($sql) or die(mysql_error());
			$sql = 'delete from ' . TABLE_PRODUCTS_DESCRIPTION . ' where products_id=' . $product_id;
			mysql_query($sql) or die(mysql_error());
			$sql = 'delete from ' . TABLE_PRODUCTS_PRICE_BREAK . ' where products_id=' . $product_id;
			mysql_query($sql) or die(mysql_error());
			$sql = 'delete from ' . TABLE_PRODUCTS_TO_CATEGORIES . ' where products_id=' . $product_id;
			mysql_query($sql) or die(mysql_error());
			$sql = 'delete from ' . TABLE_PRODUCTS_TO_DISCOUNT_CATEGORIES . ' where products_id=' . $product_id;
			mysql_query($sql) or die(mysql_error());
			$sql = 'delete from ' . TABLE_PRODUCTS_XSELL . ' where products_id=' . $product_id;
			mysql_query($sql) or die(mysql_error());
			$sql = 'delete from ' . TABLE_REVIEWS_DESCRIPTION . ' where reviews_id in (select reviews_id from ' . TABLE_REVIEWS . ' where products_id=' . $product_id . ')';
			mysql_query($sql) or die(mysql_error());
			$sql = 'delete from ' . TABLE_REVIEWS . ' where products_id=' . $product_id;
			mysql_query($sql) or die(mysql_error());
			$sql = 'delete from ' . TABLE_SPECIALS . ' where products_id=' . $product_id;
			mysql_query($sql) or die(mysql_error());
		}

		remove_cache($language_id . '_' . $product_id, 'AlsoPurchasedComponent');
		clean_cache('BestSellersComponent');
		clean_cache('CategoriesComponent');
		clean_cache('NewProductsComponent');
		remove_cache($languageId . '_' . $productId . '_true', 'ProductComponent');
		remove_cache($languageId . '_' . $productId . '_false', 'ProductComponent');
		remove_cache($language_id . '_' . $product_id, 'ProductXSellComponent');
		clean_cache('RandomReviewComponent');
		clean_cache('SpecialsComponent');
		clean_cache('UpcomingProductsComponent');
		clean_cache('WhatsNewComponent');
			
		return $this->query($params);
	}

	function copyProduct(&$params) 
	{
		$selected_product_id = (int)$params['productId'];
		$selected_category_id = (int)$params['selectedCategoryId'];
		
		$sqlQuery = 'select products_quantity, products_model, products_image, products_price, products_date_available, ' .
		'products_weight, products_virtual, products_tax_class_id, manufacturers_id, products_qty_blocks, ' .
		'product_image_1, product_image_2, product_image_3, product_image_4, product_image_5, product_image_6, ' . 
		'products_image_max_width, products_image_max_height ' .
		'from ' . TABLE_PRODUCTS . ' ' .
		'where products_id =' . $selected_product_id;
		$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
		while($row = mysql_fetch_array($dataReturned))
		{
			$sql_data_array = array();
			$sql_data_array['products_quantity'] = $row['products_quantity'];
			$sql_data_array['products_model'] = $row['products_model'];
			$sql_data_array['products_image'] = $row['products_image'];
			$sql_data_array['products_price'] = $row['products_price'];
			$sql_data_array['products_date_added'] = 'now()';
			$sql_data_array['products_last_modified'] = 'now()';
			$sql_data_array['products_date_available'] = $row['products_date_available'];
			$sql_data_array['products_weight'] = $row['products_weight'];
			$sql_data_array['products_status'] = 0;
			$sql_data_array['products_virtual'] = $row['products_virtual'];
			$sql_data_array['products_tax_class_id'] = $row['products_tax_class_id'];
			$sql_data_array['manufacturers_id'] = $row['manufacturers_id'];
			$sql_data_array['products_ordered'] = 0;
			$sql_data_array['products_qty_blocks'] = $row['products_qty_blocks'];
			$sql_data_array['product_image_1'] = $row['product_image_1'];
			$sql_data_array['product_image_2'] = $row['product_image_2'];
			$sql_data_array['product_image_3'] = $row['product_image_3'];
			$sql_data_array['product_image_4'] = $row['product_image_4'];
			$sql_data_array['product_image_5'] = $row['product_image_5'];
			$sql_data_array['product_image_6'] = $row['product_image_6'];
			$sql_data_array['products_image_max_width'] = $row['products_image_max_width'];
			$sql_data_array['products_image_max_height'] = $row['products_image_max_height'];
			db_perform(TABLE_PRODUCTS, $sql_data_array, 'insert', '');
			$product_id = mysql_insert_id();

			$sqlQuery1 = 'select language_id, products_name, products_description, products_url, products_viewed ' . 
			'from ' . TABLE_PRODUCTS_DESCRIPTION . ' ' .
			'where products_id=' . $selected_product_id . ' ' .
			'order by language_id';
			$dataReturned1 = mysql_query($sqlQuery1) or die(mysql_error());
			while($row1 = mysql_fetch_array($dataReturned1))
			{
				$sql_data_array = array();
				$sql_data_array['products_id'] = $product_id;
				$sql_data_array['language_id'] = $row1['language_id'];
				$sql_data_array['products_name'] = 'copy of ' . $row1['products_name'];
				$sql_data_array['products_description'] = $row1['products_description'];
				$sql_data_array['products_url'] = $row1['products_url'];
				$sql_data_array['products_viewed'] = $row1['products_viewed'];
				db_perform(TABLE_PRODUCTS_DESCRIPTION, $sql_data_array, 'insert', '');
			}

			$sql_data_array = array();
			$sql_data_array['products_id'] = $product_id;
			$sql_data_array['categories_id'] = $selected_category_id;
			db_perform(TABLE_PRODUCTS_TO_CATEGORIES, $sql_data_array, 'insert', '');

			$sqlQuery1 = 'select products_id, discount_categories_id ' . 
			'from ' . TABLE_PRODUCTS_TO_DISCOUNT_CATEGORIES . ' ' .
			'where products_id=' . $selected_product_id . ' ' .
			'order by discount_categories_id';
			$dataReturned1 = mysql_query($sqlQuery1) or die(mysql_error());
			while($row1 = mysql_fetch_array($dataReturned1))
			{
				$sql_data_array = array();
				$sql_data_array['products_id'] = $product_id;
				$sql_data_array['discount_categories_id'] = $row1['discount_categories_id'];
				db_perform(TABLE_PRODUCTS_TO_DISCOUNT_CATEGORIES, $sql_data_array, 'insert', '');
			}

			$sqlQuery1 = 'select products_price, products_qty ' .
			'from  ' . TABLE_PRODUCTS_PRICE_BREAK . ' ' .
			'where products_id = ' . $selected_product_id . ' ' .
			'order by products_qty';
			$dataReturned1 = mysql_query($sqlQuery1) or die(mysql_error());
			while($row1 = mysql_fetch_array($dataReturned1))
			{
				$sql_data_array = array();
				$sql_data_array['products_id'] = $product_id;
				$sql_data_array['products_price'] = $row1['products_price'];
				$sql_data_array['products_qty'] = $row1['products_qty'];
				db_perform(TABLE_PRODUCTS_PRICE_BREAK, $sql_data_array, 'insert', '');
			}

			$sqlQuery1 = 'select products_attributes_id, options_id, options_values_id, options_values_price, price_prefix, attribute_sort ' .
			'from ' . TABLE_PRODUCTS_ATTRIBUTES . ' ' .
			'where products_id=' . $selected_product_id . ' ' . 
			'order by products_attributes_id';
			$dataReturned1 = mysql_query($sqlQuery1) or die(mysql_error());
			while($row1 = mysql_fetch_array($dataReturned1))
			{
				$sql_data_array = array();
				$sql_data_array['products_id'] = $product_id;
				$sql_data_array['options_id'] = $row1['options_id'];
				$sql_data_array['options_values_id'] = $row1['options_values_id'];
				$sql_data_array['options_values_price'] = $row1['options_values_price'];
				$sql_data_array['price_prefix'] = $row1['price_prefix'];
				$sql_data_array['attribute_sort'] = $row1['attribute_sort'];
				db_perform(TABLE_PRODUCTS_ATTRIBUTES, $sql_data_array, 'insert', '');
				$product_attributes_id = mysql_insert_id();

				$sqlQuery2 = 'select products_attributes_filename, products_attributes_maxdays, products_attributes_maxcount ' .
				'from ' . TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD . ' ' .
				'where products_attributes_id=' . $row1['products_attributes_id'];
				$dataReturned2 = mysql_query($sqlQuery2) or die(mysql_error());
				while($row2 = mysql_fetch_array($dataReturned2))
				{
					$sql_data_array = array();
					$sql_data_array['products_attributes_id'] = $product_attributes_id;
					$sql_data_array['products_attributes_filename'] = $row2['products_attributes_filename'];
					$sql_data_array['products_attributes_maxdays'] = $row2['products_attributes_maxdays'];
					$sql_data_array['products_attributes_maxcount'] = $row2['products_attributes_maxcount'];
					db_perform(TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD, $sql_data_array, 'insert', '');
				}
			}

			$sqlQuery1 = 'select xsell_id, sort_order ' .
			'from ' . TABLE_PRODUCTS_XSELL . ' ' .
			'where products_id=' . $selected_product_id;
			$dataReturned1 = mysql_query($sqlQuery1) or die(mysql_error());
			while($row1 = mysql_fetch_array($dataReturned1))
			{
				$sql_data_array = array();
				$sql_data_array['products_id'] = $product_id;
				$sql_data_array['xsell_id'] = $row1['xsell_id'];
				$sql_data_array['sort_order'] = $row1['sort_order'];
				db_perform(TABLE_PRODUCTS_XSELL, $sql_data_array, 'insert', '');
			}

			$sqlQuery1 = 'select products_id, sort_order ' .
			'from ' . TABLE_PRODUCTS_XSELL . ' ' .
			'where xsell_id=' . $selected_product_id;
			$dataReturned1 = mysql_query($sqlQuery1) or die(mysql_error());
			while($row1 = mysql_fetch_array($dataReturned1))
			{
				$sql_data_array = array();
				$sql_data_array['products_id'] = $row1['products_id'];
				$sql_data_array['xsell_id'] = $product_id;
				$sql_data_array['sort_order'] = $row1['sort_order'];
				db_perform(TABLE_PRODUCTS_XSELL, $sql_data_array, 'insert', '');
			}

			clean_cache('BestSellersComponent');
			clean_cache('CategoriesComponent');
			clean_cache('NewProductsComponent');
			clean_cache('RandomReviewComponent');
			clean_cache('SpecialsComponent');
			clean_cache('UpcomingProductsComponent');
			clean_cache('WhatsNewComponent');
		}
		return $this->query($params);
	}

	function toggleProductStatus(&$params) 
	{
		$product_id = (int)$params['productId'];
		$language_id = (int)$params['languageId'];
		$sqlQuery = 'select products_status from ' . TABLE_PRODUCTS . ' where products_id=' . $product_id;
		$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
		
		while($row = mysql_fetch_array($dataReturned))
		{
			$status = (int)$row['products_status'];
			if ($status != 0) $status = 0;
			else $status = 1;
			$sql = 'update ' . TABLE_PRODUCTS . ' set products_status=' . $status . ' where products_id=' . $product_id;
			mysql_query($sql) or die(mysql_error());

			remove_cache($language_id . '_' . $product_id, 'AlsoPurchasedComponent');
			clean_cache('BestSellersComponent');
			clean_cache('CategoriesComponent');
			clean_cache('NewProductsComponent');
			clean_cache('NewProductsComponent');
			remove_cache($language_id . '_' . $product_id, 'ProductComponent');
			remove_cache($language_id . '_' . $product_id, 'ProductXSellComponent');
			clean_cache('RandomReviewComponent');
			clean_cache('SpecialsComponent');
			clean_cache('UpcomingProductsComponent');
			clean_cache('WhatsNewComponent');
			break;
		}
		return $this->query($params);
	}

	function newProductLink(&$params) 
	{
		$params['newLink'] = true;
		return $this->moveProduct($params);
	}

	function moveProduct(&$params) 
	{
		$product_id = (int)$params['productId'];
		$language_id = (int)$params['languageId'];
		$previous_category_id = (int)$params['previousCategoryId'];
		$selected_category_id = (int)$params['selectedCategoryId'];
		$newLink = (boolean)$params['newLink'];

		if ($product_id > 0 && $previous_category_id != $selected_category_id)
		{	
			if ($selected_category_id == 0)  {
				$j=1;
			}
			else {
				$sqlQuery = 'select categories_id from ' . TABLE_CATEGORIES . ' where categories_id=' . $selected_category_id;
				$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
				$j = 0;
				while($row = mysql_fetch_array($dataReturned))
				{
					$j++;
				}
			}

			if ($j == 0)
			{
				$this->errorCode = -110;
				$this->message = 'PARENT_DOES_NOT_EXIST';
				return $this->query($params);
			}

			$sqlQuery = 'select categories_id from ' . TABLE_CATEGORIES . ' where parent_id=' . $selected_category_id;
			$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
			$i = 0;
			while($row = mysql_fetch_array($dataReturned))
			{
				$i++;
				break;
			}
			
			if ($i > 0)
			{
				$this->errorCode = -140;
				$this->message = 'PARENT_CONTAINS_CATEGORIES';
				return $this->query($params);
			}

			$sqlQuery = 'select products_id from ' . TABLE_PRODUCTS_TO_CATEGORIES . ' where categories_id=' . $selected_category_id . ' and products_id =' . $product_id;
			$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
			$i = 0;
			while($row = mysql_fetch_array($dataReturned))
			{
				$i++;
				break;
			}
			
			if ($i > 0)
			{
				$this->errorCode = -150;
				$this->message = 'PARENT_ALREADY_CONTAINS_PRODUCT';
				return $this->query($params);
			}

			$sqlQuery = 'select products_id from ' . TABLE_PRODUCTS_TO_CATEGORIES . ' where categories_id=' . $previous_category_id . ' and products_id =' . $product_id;
			$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
			$i = 0;
			while($row = mysql_fetch_array($dataReturned))
			{
				$i++;
				break;
			}
			
			if ($i == 0)
			{
				$this->errorCode = -160;
				$this->message = 'PREVIOUS_PARENT_DOES_NOT_CONTAIN_PRODUCT';
				return $this->query($params);
			}
			if ($newLink) {
				$sqlQuery = 'Insert into ' . TABLE_PRODUCTS_TO_CATEGORIES . ' (categories_id, products_id) values(' . $selected_category_id . ', ' . $product_id .')';
			} else {
				$sqlQuery = 'Update ' . TABLE_PRODUCTS_TO_CATEGORIES . ' set categories_id=' . $selected_category_id . ' where categories_id=' . $previous_category_id . ' and products_id =' . $product_id;
			}
			mysql_query($sqlQuery) or die(mysql_error());

			remove_cache($language_id . '_' . $product_id, 'AlsoPurchasedComponent');
			clean_cache('BestSellersComponent');
			clean_cache('CategoriesComponent');
			clean_cache('NewProductsComponent');
			clean_cache('NewProductsComponent');
			remove_cache($language_id . '_' . $product_id, 'ProductComponent');
			remove_cache($language_id . '_' . $product_id, 'ProductXSellComponent');
			clean_cache('RandomReviewComponent');
			clean_cache('SpecialsComponent');
			clean_cache('UpcomingProductsComponent');
			clean_cache('WhatsNewComponent');
		}
		$params['categoryId'] = 0;
		$params['selectedCategoryId'] = $selected_category_id;

		return $this->query($params);
	}

}
?>

